/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author huwenqing
 * @date 2016年10月19日
 *
 */

package com.eyaomall.wxmall.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.aliyun.api.gateway.logisticstracking.model.QueryExpressInfoResult;
import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.util.ExpressInfoUtils;
import com.eyaomall.wxmall.model.po.Logistics;
import com.eyaomall.wxmall.service.LogisticsService;
import com.lycheeframework.core.exception.ErrorCodeException;

@RequestMapping("logistics")
@Controller
public class LogisticsController {
	// APP KEY
	private static String APP_KEY = "23484132";
	// APP密钥
	private static String APP_SECRET = "647c5ad949a579c85ea1d350714dc29a";

	@Autowired
	private LogisticsService logisticsService;

	/**
	 * 获取对象数据
	 * 
	 * @param model
	 * @param logistics
	 * @return
	 */
	@RequestMapping("page")
	public String pageData(Integer orderId, Map<String, Object> model) {
		QueryExpressInfoResult queryExpressInfoResult;
		ExpressInfoUtils.setAPPINFO(APP_KEY, APP_SECRET);
		try {

			Logistics logis = (Logistics) logisticsService.queryByOrderId(orderId);

			queryExpressInfoResult = ExpressInfoUtils.queryExpress(logis.getNumber(), "AUTO");

			// 查询信息返回Null
			if (queryExpressInfoResult == null) {
				throw new ErrorCodeException(BECode.logistics_8200001);
			}
			Logistics logistics = new Logistics();
			logistics.setNumber(queryExpressInfoResult.getNumber());
			logistics.setName(queryExpressInfoResult.getType());
			logistics.setStatus(queryExpressInfoResult.getDeliverystatus());
			logistics.setDetails(JSON.toJSONString(queryExpressInfoResult.getList()));
			logistics.setOrderId(orderId);
			logisticsService.update(logistics);

			Logistics details = (Logistics) logisticsService.queryByOrderId(orderId);

			model.put("logistics", logisticsService.queryByOrderId(orderId));
			model.put("details", queryExpressInfoResult.getList());
		} catch (Exception e) {
			throw new ErrorCodeException(BECode.logistics_8200002);
		}
		return "logistics/log_index";
	}

}
